﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using RealtorsPortal.Entities;
using System.Data.SqlClient;

namespace RealtorsPortal.Data_Access_Layer
{
    public class RoleDAL
    {

        DataAccessHelper dah = new DataAccessHelper();

        public DataTable GetRoleDataTable(){
            return dah.GetDataTable("select * from Roles");
        }

        public List<Role> GetRolesList()
        {
            DataTable dt = this.GetRoleDataTable();
            List<Role> list = new List<Role>();
            if (dt.Rows.Count>0)
            {
                foreach(DataRow r in dt.Rows){
                    Role role = new Role();
                    role.RoleId = (int)r["RoleId"];
                    role.RoleName = r["RoleName"].ToString();
                    role.Description = r["RoleDescription"].ToString();
                    list.Add(role);
                }
            }
            return list;
        }

        public Role GetRoleById(int roleId)
        {
            DataTable dt = dah.GetDataTable("select * from Roles where RoleId = " + roleId);
            DataRow r = dt.Rows[0];
            Role role = new Role();
            role.RoleId = (int)r["RoleId"];
            role.RoleName = r["RoleName"].ToString();
            role.Description = r["RoleDescription"].ToString();

            return role;
        }

        public bool AddRole(Role r) {
            List<SqlParameter> paraList = new List<SqlParameter>();
            paraList.Add(new SqlParameter("@name",r.RoleName));
            paraList.Add(new SqlParameter("@description", r.Description));
            return dah.ExcuteStoredNonQuery("sp_InsertRole", paraList)>0;
        }

        public bool UpdateRole(Role r) {
            List<SqlParameter> paraList = new List<SqlParameter>();
            paraList.Add(new SqlParameter("@roleid", r.RoleId));
            paraList.Add(new SqlParameter("@name", r.RoleName));
            paraList.Add(new SqlParameter("@description", r.Description));
            return dah.ExcuteStoredNonQuery("sp_UpdateRole", paraList) > 0;
        }

        public bool DeleteRole(int roleId)
        {
            List<SqlParameter> paraList = new List<SqlParameter>();
            paraList.Add(new SqlParameter("@roleid", roleId));
            return dah.ExcuteStoredNonQuery("sp_deleteRole", paraList) > 0;
        }
    }
}